/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.sfo.viewer.designer;


import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;


/**
 * Wrapper class for a DesignerElement.
 * <p/>
 * @author renke002
 */
public class DesignerElementNode extends AbstractNode
//public class DesignerElementNode extends BeanNode<DesignerElement>
{

    /**
     * Attributes for <gids-designer:cell-info>.
     */
    public final static String[][] GLOBAL_PROPS =
    {
        {
            "Label" , "getLabel" , "setLabel"
        } ,
        {
            "Row(s)" , "getRowString" , null
        } ,
        {
            "Column(s)" , "getColString" , null
        } ,
        {
            "Width" , "getWidth" , null
        } ,
        {
            "Height" , "getHeight" , null
        }
    };
    /**
     * Position of the attribute name in the Designer Element properties array.
     */
    public final static int PROPS_ARRAY_NAME = 0;
    /**
     * Position of the getter function name in the Designer Element properties
     * array.
     */
    public final static int PROPS_ARRAY_GETTER = 1;
    /**
     * Position of the setter function name in the Designer Element properties
     * array.
     */
    public final static int PROPS_ARRAY_SETTER = 2;


    /**
     *
     * @param dElt
     */
    public DesignerElementNode( DesignerElement dElt )
    {
        super( Children.LEAF , Lookups.fixed( new Object[]
        {
            dElt
        } ) );
        if ( dElt.containsText() )
        {
            this.setDisplayName( dElt.getText() );
        }
        else
        {
            this.setDisplayName( dElt.getLabel() );
        }
    }

//    public DesignerElementNode( DesignerElement dElt )
//              throws IntrospectionException
//    {
//        super( dElt );
//        this.setDisplayName( dElt.getLabel() );
//    }

    /**
     *
     * @return
     */
    @Override
    protected Sheet createSheet()
    {
        //
        Sheet sheet = Sheet.createDefault();
        //         Sheet.Set set = Sheet.createPropertiesSet();

        DesignerElement dElt = getLookup().lookup( DesignerElement.class );

        try
        {
            //
            sheet.put( createSheetMainProps( dElt ) );
        }
        catch ( NoSuchMethodException ex )
        {
            Exceptions.printStackTrace( ex );
        }

        return sheet;
    }


    /**
     *
     * @param dElt
     * @return
     * @throws NoSuchMethodException
     */
    private Sheet.Set createSheetMainProps( DesignerElement dElt )
            throws NoSuchMethodException
    {
        Sheet.Set mainSheet = Sheet.createPropertiesSet();

        Property prop;

        for ( int i = 0; i < GLOBAL_PROPS.length; i++ )
        {
            prop = new PropertySupport.Reflection(
                    dElt ,
                    String.class ,
                    GLOBAL_PROPS[i][DesignerElementNode.PROPS_ARRAY_GETTER] ,
                    GLOBAL_PROPS[i][DesignerElementNode.PROPS_ARRAY_SETTER] );

            prop.setName(
                    GLOBAL_PROPS[i][DesignerElementNode.PROPS_ARRAY_NAME] );
            mainSheet.put( prop );
        }

        return mainSheet;
    }

}
